package com.dispatcher;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;

import org.json.JSONException;

import com.dispatcher.bean.Order;
import com.dispatcher.http.HttpHelperWraper;
import com.dispatcher.http.HttpHelperWraper.HttpRequestParam;
import com.dispatcher.http.HttpHelperWraper.OnNetListener;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class RegisterActivity extends Activity implements OnNetListener, OnClickListener {
	EditText etPhone;
	EditText etName;
	Button btSubmit;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_register);
		etPhone = (EditText) findViewById(R.id.et_phone);
		etName = (EditText) findViewById(R.id.et_name);
		btSubmit = (Button) findViewById(R.id.bt_submit);
		btSubmit.setOnClickListener(this);
	}
	
	void sendRequest(String phone, String name) {
		HttpHelperWraper http = new HttpHelperWraper();
		HttpRequestParam param = http.getParam();
		makeparam(param, phone, name);
		http.AsyncTask(param);
	}
	
	void makeparam(HttpRequestParam param, String phone, String name){
		if (param == null) {
			return;
		}
		param.ctx = this;
		param.listener = this;
		
		HashMap<String, Object> requestparams = new HashMap<String, Object>();

		requestparams.put("task", "registerPresent");
		requestparams.put("name", name);
		requestparams.put("phone", phone);
		param.nodesrequest = requestparams;
	}
	
	@Override
	public void onNetSuccess(HttpRequestParam param) {
		Log.i("liutao", param.nodesrequest.toString());
		try {
			String pid=String.valueOf(param.nodesresult.get("presentid"));
			savePresentId(pid);
			GlobalVar gvar = (GlobalVar) getApplication();
			gvar.initDBCtrl(pid);
			startActivity(new Intent(this, HomeTabActivity.class));
			
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void onNetError(HttpRequestParam param) {
		Toast.makeText(this, param.rstmsg, 2000).show();
	}

	OnClickListener leftlis = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
		}
	};
	
	OnClickListener rightlis = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
		}
	};
	
	// 保存到sd卡
	private void savePresentId(String presentId) {
		if (Environment.getExternalStorageState().equals(Environment.MEDIA_UNMOUNTED)) {
			return;
		}
		String sdcard = Environment.getExternalStorageDirectory().getAbsolutePath();
		//先判断目录是否存在
		File dir = new File(sdcard + "/dayinxiang");
		if (!dir.exists()) { //不存在则创建
			dir.mkdirs();
		}
		//判断文件是否存在
		File file = new File(sdcard + "/dayinxiang" + "/" + "presentid.txt");
		if (!file.exists()) { //不存在则保存
			try {
				
				file.createNewFile();
				
		        BufferedWriter output = new BufferedWriter(new FileWriter(file));
		        output.write(presentId);
		        output.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	@Override
	public void onClick(View v) {
		String phone = etPhone.getText().toString().trim();
		String name = etName.getText().toString().trim();
		
		if(!TextUtils.isEmpty(phone) && !TextUtils.isEmpty(name)) {
			sendRequest(phone, name);
		}
		
	}
}
